package com.cpi.cornpp.manualcharges.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.dto.PurchaseOrderVersionDTO;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cs.common.utils.NumberUtil;

/**
 * Detalle de Cargos Manuales
 * 
 * @author Edgar Joao
 *	
 */
public class ManualChargesDetailsConverter {
	public static final char BALANCED = 'P';
	public static final char UNIT     = 'U';
	public static final char MANUAL   = 'M';	
	/**
	 * Convierte una lista de Ordenes de compra
	 * segun el tipo.
	 *
	 * @author Edgar Joao	
	 * @param purchaseOrderList
	 * @param chargeType U - Unitario, M - Manual y P Ponderado
	 * @return
	 * @throws PurchaseProgramException 
	 */
	public List convertPurchaseOrderVersion(char chargeType, List purchaseOrderList, String amount) throws PurchaseProgramException{
		List viewList = null;					
		try{
			switch (chargeType) {
			case BALANCED:
					viewList = this.manualChargeDetailPType(purchaseOrderList);
				 break;
			case UNIT:
					viewList = this.manualChargeDetailUType(purchaseOrderList, amount);
				 break;
			case MANUAL:
					viewList = this.manualChargeDetailMType(purchaseOrderList);
				 break;
			}
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return viewList;
	}
	/**
	 * Construye la lista de tipo Unitario
	 * 
	 * @author Edgar Joao
	 * @param listTypeU
	 * @return
	 * @throws PurchaseProgramException
	 */
	private List manualChargeDetailUType(List listTypeU, String amount) throws PurchaseProgramException{
		List resultList = null;
		Iterator iterator = null;
		PurchaseOrderVersionView view = null;
		PurchaseOrderVersionDTO dto = null;		
		StringBuffer concatenate = null;
		try{
			resultList = new ArrayList(0);
			iterator = listTypeU.iterator();
			while(iterator.hasNext()){				 
				dto = (PurchaseOrderVersionDTO) iterator.next();
				view = new PurchaseOrderVersionView();
				concatenate = new StringBuffer(0);
				view.setAmount(NumberUtil.convertPrice(dto.getAmount()));
				view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
				view.setPeriodDescription(dto.getPeriodDescription());
				concatenate.append(dto.getCode());
				concatenate.append(String.valueOf(NumberUtil.fill(dto.getCodeNumber().intValue(), '0', 2)));
				view.setCode(concatenate.toString());
				view.setCapacityStringValue(NumberUtil.convertPrice(dto.getCapacity()));
				view.setCapacity(dto.getCapacity());				
				view.setUnitPrice(amount);
				resultList.add(view);
			}			
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return resultList;
	}
	/**
	 * Construye la lista de tipo Ponderado
	 * 
	 * @author Edgar Joao
	 * @param listTypeP
	 * @return
	 * @throws PurchaseProgramException
	 */
	private List manualChargeDetailPType(List listTypeP) throws PurchaseProgramException{
		List resultList = null;
		Iterator iterator = null;
		PurchaseOrderVersionView view = null;
		PurchaseOrderVersionDTO dto = null;		
		StringBuffer concatenate = null;
		try{
			resultList = new ArrayList(0);					
			iterator = listTypeP.iterator();
			while(iterator.hasNext()){
				dto = (PurchaseOrderVersionDTO) iterator.next();
				view = new PurchaseOrderVersionView();
				concatenate = new StringBuffer(0);
				view.setAmount(NumberUtil.convertPrice(dto.getAmount()));
				view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
				view.setPeriodDescription(dto.getPeriodDescription());
				concatenate.append(dto.getCode());
				concatenate.append(String.valueOf(NumberUtil.fill(dto.getCodeNumber().intValue(), '0', 2)));
				view.setCode(concatenate.toString());
				view.setCapacityStringValue(NumberUtil.convertPrice(dto.getCapacity()));
				view.setCapacity(dto.getCapacity());				
				view.setUnitPrice(NumberUtil.convertPrice( dto.getCapacity().doubleValue() / dto.getAmount()));
				resultList.add(view);
			}					
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return resultList;
	}
	/**
	 * Construye la lista de tipo Manual
	 * 
	 * @author Edgar Joao
	 * @param listTypeM
	 * @return
	 * @throws PurchaseProgramException
	 */
	private List manualChargeDetailMType(List listTypeM) throws PurchaseProgramException{
		List resultList = null;
		Iterator iterator = null;
		PurchaseOrderVersionView view = null;
		PurchaseOrderVersionDTO dto = null;		
		StringBuffer concatenate = null;
		try{
			resultList = new ArrayList(0);
			iterator = listTypeM.iterator();
			while(iterator.hasNext()){
				dto = (PurchaseOrderVersionDTO) iterator.next();
				view = new PurchaseOrderVersionView();
				concatenate = new StringBuffer(0);
				view.setAmount(NumberUtil.convertPrice(dto.getAmount()));
				view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
				view.setPeriodDescription(dto.getPeriodDescription());
				concatenate.append(dto.getCode());
				concatenate.append(String.valueOf(NumberUtil.fill(dto.getCodeNumber().intValue(), '0', 2)));
				view.setCode(concatenate.toString());
				view.setCapacityStringValue(NumberUtil.convertPrice(dto.getCapacity()));
				view.setCapacity(dto.getCapacity());				
				view.setUnitPrice(NumberUtil.convertPrice( dto.getCapacity() ));
				resultList.add(view);
			}			
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}		
		return resultList;		
	}
}
